package com.example.qihuibao.widget.dialog

import android.content.DialogInterface
import android.view.View
import android.widget.*
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.qihuibao.R
import com.example.qihuibao.base.BaseDialogFragment
import com.example.qihuibao.bean.RspQueryCanteenInfoBean
import com.example.qihuibao.widget.view.CommonRecyclerView
import java.lang.StringBuilder


/**
 *@author: cww
 *@desc:
 *@date: 5/16/23 10:58 AM
 */
class CanteenSelectDialog(private val list:List<RspQueryCanteenInfoBean>?, private val dismissBack:(pos:Int)->Unit) : BaseDialogFragment(R.layout.dialog_list_select) {
    private var index = -1

    override fun initView() {
        mView?.findViewById<TextView>(R.id.tv_title)?.text = "选择餐厅"
        val rvCanteen = mView?.findViewById<CommonRecyclerView>(R.id.rv_canteen)
        rvCanteen?.init(
            R.layout.item_canteen,
            LinearLayoutManager(context, RecyclerView.VERTICAL, false),
            list,
            {parent, pos ->
                parent.findViewById<TextView>(R.id.tv_name).text = getString(R.string.canteen_name, list!![pos].CantName)
                parent.findViewById<TextView>(R.id.tv_status).text = getString(R.string.canteen_status, if (list!![pos].Status=="0") "正常" else "冻结")
                parent.findViewById<TextView>(R.id.tv_order_node).text = getString(R.string.order_mode, getOrderMode(list!![pos]))
                parent.findViewById<TextView>(R.id.tv_use_node).text = getString(R.string.use_mode, getUseMode(list!![pos]))
                parent.findViewById<TextView>(R.id.tv_flow_type).text = getString(R.string.flow_type, getFlowType(list!![pos]))
                parent.findViewById<ImageView>(R.id.iv_select).visibility = if (index == pos) View.VISIBLE else View.GONE
            },
            {
                index = it
                rvCanteen?.adapter?.notifyDataSetChanged()
            }
        )
        mView?.findViewById<View>(R.id.btn_ok)?.setOnClickListener { dismiss() }
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        if (index == -1) {
            index = 0
        }
        dismissBack.invoke(index)
    }

    private fun getOrderMode(bean: RspQueryCanteenInfoBean): String {
        return when(bean.OrderMode){
            "1" -> "套餐"
            "2" -> "点餐"
            "3" -> "简约"
            "4" -> "混合"
            else -> ""
        }
    }

    private fun getUseMode(bean: RspQueryCanteenInfoBean): String {
        if (bean.UseMode.isNullOrEmpty() || bean.UseMode == "0") return "不开通"
        val charArray = bean.UseMode!!.toCharArray()
        val sb = StringBuilder()
        for (i in charArray.indices) {
            when(i) {
                0 -> sb.append("出示取餐信息-${if (charArray[i] == '0') "不开通" else "开通"},")
                1 -> sb.append("扫码取餐-${if (charArray[i] == '0') "不开通" else "开通"},")
                2 -> sb.append("系统对接-${if (charArray[i] == '0') "不开通" else "开通"}")
            }
        }
        return sb.toString()
    }

    private fun getFlowType(bean: RspQueryCanteenInfoBean): String {
        return when(bean.FlowType){
            //0 自动,1 客户确认,2 客服确认,3 客服-客户确认,4 客户-客服确认
            "0" -> "自动"
            "1" -> "客户确认"
            "2" -> "客服确认"
            "3" -> "客服-客户确认"
            "4" -> "客户-客服确认"
            else -> ""
        }
    }

}